<?php
/**
 * @file 1.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-04-30 02:04:27 +0000 (Tue, 30 Apr 2013) $
 *
 * $Id: 1.php 63 2013-04-30 02:04:27Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/install/steps/1.php $
 */

if(defined('__INSTALLER__')) {
	if(is_readable($wcPaths['config-local']) || is_readable($wcPaths['install-tmps']['db'])) {
		echo "\t\t\t\t<p>Database already configured.</p>\n";
		$isOkToReload = true;
	} else {
		if($_SERVER["REQUEST_METHOD"] != "POST") {
			echo "\t\t\t\t<p>This is step {$wciStepCheck}. Here you will configure the database.</p>\n";
			echo "\t\t\t\t<p><form action=\"#\" method=\"post\">\n";
			echo "\t\t\t\t\t<ul>\n";
			echo "\t\t\t\t\t\t<li><label>Server</label>:<input type=\"text\" id=\"DB_SERVER\" name=\"DB_SERVER\" value=\"localhost\"/></li>\n";
			echo "\t\t\t\t\t\t<li><label>Database Name</label>:<input type=\"text\" id=\"DB_NAME\" name=\"DB_NAME\" value=\"\"/></li>\n";
			echo "\t\t\t\t\t\t<li><label>Tables Prefix</label>:<input type=\"text\" id=\"DB_PREFIX\" name=\"DB_PREFIX\" value=\"wc_\"/></li>\n";
			echo "\t\t\t\t\t\t<li><label>Database Username</label>:<input type=\"text\" id=\"DB_USERNAME\" name=\"DB_USERNAME\" value=\"\"/></li>\n";
			echo "\t\t\t\t\t\t<li><label>Database Password</label>:<input type=\"password\" id=\"DB_PASSWORD\" name=\"DB_PASSWORD\" value=\"\"/></li>\n";
			echo "\t\t\t\t\t</ul>\n";
			echo "\t\t\t\t\t<input type=\"submit\" value=\"Send\"/>\n";
			echo "\t\t\t\t\t<input type=\"reset\"  value=\"Reset\"/>\n";
			echo "\t\t\t\t</form></p>\n";

			$isOkToContinue = false;
			$isOkToReload = false;
		} else {
			try {
				$dblink = new PDO("mysql:host={$_POST["DB_SERVER"]};dbname={$_POST["DB_NAME"]}", $_POST["DB_USERNAME"], $_POST["DB_PASSWORD"]);
			} catch (Exception $e) {
				$dblink = false;
			}

			if($dblink) {
				$aux = "<?php\n";
				$aux .= "\$wcDatabaseAccess['server']	= '{$_POST["DB_SERVER"]}';\n";
				$aux .= "\$wcDatabaseAccess['username']	= '{$_POST["DB_USERNAME"]}';\n";
				$aux .= "\$wcDatabaseAccess['password']	= '{$_POST["DB_PASSWORD"]}';\n";
				$aux .= "\$wcDatabaseAccess['database']	= '{$_POST["DB_NAME"]}';\n";
				if($_POST["DB_PREFIX"] != $wcDatabaseAccess['prefix']) {
					$aux .= "\$wcDatabaseAccess['prefix']	= '{$_POST["DB_PREFIX"]}';\n";
				}

				$aux .= "\nrequire_once \"{\$wcROOTDIR}/config/post.config.php\";\n";
				$aux .= "?>\n";

				file_put_contents($wcPaths['install-tmps']['db'], $aux);

				echo "\t\t\t\t<p>[OK:Database connection parameters were accepted.]</p>\n";
			} else {
				echo "\t\t\t\t<p>[FL:Database connection parameters were wrong.]</p>\n";
				$isOkToContinue = false;
				$isOkToReload = true;
			}
		}
	}
}
?>
